package mylibrary.socket.proxy;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

/**
 * This is for HTTP proxy
 * @author gao
 *
 */
public class Proxy {

	
	public static int CR = 13;
	public static int LF = 10;
	
	List headers = new ArrayList();
	//String body = null;
	
	public static void main(String[] args) throws Exception {
		
		ServerSocket serverSocket = new ServerSocket(8085);
		Socket socket = serverSocket.accept();
		
		Proxy proxy = new Proxy();
		proxy.parseRequest2(socket);
	}
	
	public void parseRequest(Socket socket) throws Exception {
		
		InputStream is = socket.getInputStream();
		
		byte[] content = new byte[1]; 
		int count = 0;
		List<Byte> temp_header = new ArrayList<Byte>(); 
		List<Byte> temp_body = new ArrayList<Byte>();
		boolean headerDone = false;
		
		while((count=is.read(content)) != -1) {
			if(headerDone) {
				temp_body.add(content[0]);
			} else {
				if(content[0] == CR) {
					byte nextByte = 0;
					is.read(content);
					nextByte = content[0];
					
					if(nextByte == LF) {
						byte thirdByte = 0;
						is.read(content);
						thirdByte = content[0];
						
						byte fourthByte = 0;
						is.read(content);
						fourthByte = content[0];
						
						if(thirdByte==CR && fourthByte==LF) {
							//start of body
							System.out.println("End of the header part");
							headerDone = true;
							continue;
						} else {
							//next line in header
							byte[] header = new byte[temp_header.size()];
							for(int i=0; i<temp_header.size(); i++) {
								header[i] = temp_header.get(i);
							}
							headers.add(new String(header));

							System.out.println("new line in header");
							
							temp_header.clear();
							temp_header.add(thirdByte);
							temp_header.add(fourthByte);
						} 
					} else {
						temp_header.add(nextByte);
						System.out.println("Guess will not call");
					}
				} else {
					temp_header.add(content[0]);
				}
			}
		}
		
		byte[] body = new byte[temp_body.size()];
		for(int i=0; i<temp_body.size(); i++) {
			body[i] = temp_body.get(i);
		}
		
		for(int i=0; i<headers.size(); i++) {
			System.out.println(headers.get(i));
		}
		System.out.println("------------------");
		
		System.out.println(new String(body));
	}

	public void parseRequest2(Socket socket) throws Exception {
		InputStream is = socket.getInputStream();
		
		byte[] content = new byte[4096];
		int bytesRead;
		while( (bytesRead = is.read(content)) != -1 ) {
			System.out.println("request read done!");
			break;
		}
		
		//parse the request byte
		List<Byte> temp_header = new ArrayList<Byte>(); 
		List<Byte> temp_body = new ArrayList<Byte>();
		boolean headerDone = false;
		for(int i=0; i<content.length; i++) {
			
			if(headerDone) {
				temp_body.add(content[i]);
			} else {
				if(content[i] == CR) {
					byte nextByte = 0;
					//is.read(content);
					nextByte = content[i+1];
					
					if(nextByte == LF) {
						byte thirdByte = 0;
						//is.read(content);
						thirdByte = content[i+2];
						
						byte fourthByte = 0;
						//is.read(content);
						fourthByte = content[i+3];
						
						if(thirdByte==CR && fourthByte==LF) {
							//start of body
							System.out.println("End of the header part");
							headerDone = true;
							continue;
						} else {
							//next line in header
							byte[] header = new byte[temp_header.size()];
							for(int j=0; j<temp_header.size(); j++) {
								header[j] = temp_header.get(j);
							}
							headers.add(new String(header));

							System.out.println("new line in header");
							
							temp_header.clear();
							//temp_header.add(thirdByte);
							//temp_header.add(fourthByte);
						} 
					} else {
						temp_header.add(nextByte);
						System.out.println("Guess will not call");
					}
				} else {
					temp_header.add(content[i]);
				}
			}
		}
		
		
		System.out.println("------------- Header ------------");		
		for(int i=0; i<headers.size(); i++) {
			System.out.println(headers.get(i));
		}
		
		byte[] body = new byte[temp_body.size()];
		for(int i=0; i<temp_body.size(); i++) {
			body[i] = temp_body.get(i);
		}
		
		
		System.out.println("------body---------");
		
		//System.out.println("body=" + new String(body));
		
		OutputStream os = socket.getOutputStream();
		String s = "gaoshuaidage";
		os.write(s.getBytes());
		
		is.close();
		os.close();
		socket.close();
	}
}

